package com.qzsoft.zhjd.machine.modules.machineinfo.controller;

import java.util.List;
import java.util.Map;

import com.qzsoft.jeemis.common.utils.ConvertDictUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.jeemis.common.validator.group.AddGroup;
import com.qzsoft.jeemis.common.validator.group.UpdateGroup;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.utils.ResultUtils;
import com.qzsoft.zhjd.machine.modules.machineinfo.dto.MachineInfoBookListDto;
import com.qzsoft.zhjd.machine.modules.machineinfo.entity.MachineInfoEntity;
import com.qzsoft.zhjd.machine.modules.machineinfo.service.MachineInfoService;

/**
 * @ClassName MachineInfoController
 * @Description 机械设备控制器
 * @author linhuawei
 * @since 2021-11-01
 */
@RestController
@RequestMapping("/api/machineInfo")
public class MachineInfoController {

    @Autowired
    private MachineInfoService machineInfoService;
    
    /**
     * @Title page
     * @Description 获取机械设备分页列表
     * @param params 查询条件
     * @return 机械设备分页列表
     */
    @RequiresPermissions("api:machineinfo:list")
    @GetMapping("/page")
    public Result<IPage<MachineInfoEntity>> page(@RequestParam Map<String, Object> params) {
        IPage<MachineInfoEntity> page = machineInfoService.page(params);
        return ResultUtils.ok(page);
    }
    
    /**
     * @Title bookPage
     * @Description 设备台账分页数据
     * @param params 查询条件
     * @return 设备台账分页数据
     */
    @GetMapping("/bookPage")
    public Result<IPage<MachineInfoBookListDto>> bookPage(@RequestParam Map<String, Object> params){
        IPage<MachineInfoBookListDto> page = machineInfoService.bookPage(params);
        return ResultUtils.ok(page);
    }

    /**
     * @Title list
     * @Description 获取机械设备数据列表
     * @param params 查询条件
     * @return 机械设备数据列表
     */
    @GetMapping("/list")
    public Result<List<MachineInfoEntity>> list(@RequestParam Map<String, Object> params) {
        List<MachineInfoEntity> list = machineInfoService.list(params);
        return ResultUtils.ok(list);
    }

    /**
     * @Title: info
     * @Description: 获取机械设备
     * @param id 机械设备主键
     * @return 机械设备
     */
    @RequiresPermissions("api:machineinfo:info")
    @GetMapping("/info/{id}")
    public Result<MachineInfoEntity> info(@PathVariable("id") String id) {
        MachineInfoEntity entity = machineInfoService.getById(id);
        ConvertDictUtils.formatDicData(entity);
        if (null == entity) {
            return ResultUtils.error(BizCode.DATA_NOT_FOUND);
        }
        return ResultUtils.ok(entity);
    }

    /**
     * @Title: info
     * @Description: 获取机械设备详情--修改
     * @param id 机械设备主键
     * @return 机械设备
     */
    @RequiresPermissions("api:machineinfo:edit")
    @GetMapping("/edit/{id}")
    public Result<MachineInfoEntity> edit(@PathVariable("id") String id) {
        MachineInfoEntity entity = machineInfoService.getById(id);
        if (null == entity) {
            return ResultUtils.error(BizCode.DATA_NOT_FOUND);
        }
        return ResultUtils.ok(entity);
    }

    /**
     * @Title: save
     * @Description: 添加机械设备
     * @param entity 机械设备
     * @return 添加结果
     */
    @RequiresPermissions("api:machineinfo:save")
    @PostMapping("/save")
    public Result<String> save(@RequestBody @Validated(AddGroup.class) MachineInfoEntity entity) {
        machineInfoService.save(entity);
        return ResultUtils.ok();
    }

    /**
     * @Title: update
     * @Description: 修改机械设备
     * @param entity 机械设备
     * @return 修改结果
     */
    @PostMapping("/update")
    public Result<String> update(@RequestBody @Validated(UpdateGroup.class) MachineInfoEntity entity) {
        machineInfoService.updateById(entity);
        return ResultUtils.ok();
    }

    /**
     * @Title: delete
     * @Description: 删除机械设备
     * @param id 机械设备主键
     * @return 删除结果
     */
    @RequiresPermissions("api:machineinfo:remove")
    @PostMapping("/delete/{id}")
    public Result<String> delete(@PathVariable("id") String id) {
        machineInfoService.removeById(id);
        return ResultUtils.ok();
    }

}
